<template>
    <div class="search-form isheader" v-if="headerShow">
        <div>
            <a-form size="small" :model="pageData" layout="inline">
                <a-form-item label="入库单号">
                    <a-input allow-clear :style="{ width: '200px' }" class="bacClass" v-model="pageData.e" />
                </a-form-item>
                <CorporateSectorCascader :configCascader="configCascader2" @changeCascader="changeCascader2" />
                <a-form-item label="仓库">
                    <a-select popup-container="html"  class="bacClass" :style="{ width: '10rem' }" v-model="pageData.b" allow-clear>
                        <a-option v-for="item in fhWhList" :value="item.id">
                            {{ item.warehouseName }}
                        </a-option>
                    </a-select>
                </a-form-item>
            </a-form>


        </div>
        <div>
            <Toolbar :items="toolbarList" @select="selectHandler" @reders="redersOut" @del="delSles" @audit="audits">
            </Toolbar>
        </div>
    </div>
    <div class="my-divisders">
        <a-divider orientation="center">
            <icon-double-up v-if="headerShow" @click="headerShow = false" :strokeWidth="2" :size="18" />
            <icon-double-down v-else @click="headerShow = true" :strokeWidth="2" :size="18" />
        </a-divider>
    </div>
    <div class="gang"></div>
    <div :style="{ height: '86.8%', padding: '10px' }">
        <a-tabs   :lazy-load="true" :justify="true" :default-active-key="tabsKeys" @change="tabsChanges">
            <a-tab-pane title="待处理" :key="2">
                <AgGridVuesCopys :rowHeight="33" :headerHeight="33" :suppressCopyRowsToClipboard="true" rowSelection="multiple" @grid-ready="gridReady2"
                    style="height:94%" :defaultColDef="defaultColDef2" class='ag-theme-alpine' :rowData="tableDataList2"
                    :columnDefs="tc" :localeText="localeText1"></AgGridVuesCopys>
                <paginationCopys :total="fySelet.total" show-total show-jumper show-page-size @change="PageSize"
                    @page-size-change="change" style="justify-content: center" :page-size="fySelet.size"
                    :current="fySelet.page" />
            </a-tab-pane>
            <a-tab-pane title="已处理" :key="1">
                <AgGridVuesCopys :rowHeight="33" :headerHeight="33" :suppressCopyRowsToClipboard="true" rowSelection="multiple" @grid-ready="gridReady"
                    style="height:94%" :defaultColDef="defaultColDef" class='ag-theme-alpine' :rowData="tableDataList"
                    :columnDefs="tc" :localeText="localeText1"></AgGridVuesCopys>
                <paginationCopys :total="fySelet2.total" show-total show-jumper show-page-size @change="PageSize2"
                    @page-size-change="change2" style="justify-content: center" :page-size="fySelet2.size"
                    :current="fySelet2.page" />
            </a-tab-pane>

        </a-tabs>
    </div>
    <a-modal v-model:visible="cgOut.visible" :on-before-ok="cgHandleOk" @cancel="cadelvs" fullscreen :mask-closable="false">
        <template #title>
            {{ cgOut.title }}
        </template>
        <div v-if="tabsKeys == 2" :style="{ marginBottom: '10px' }">
            <a-form ref="formXl" size="small" :model="pageData2" layout="inline">
                <CorporateSectorCascader :configCascader="configCascader2" @changeCascader="changeCascader3" />
                <a-form-item label="仓库">
                    <a-select popup-container="html"  class="bacClass" :style="{ width: '10rem' }" @change="warehouseChange"
                        v-model="warehouseNameAanId" allow-clear>
                        <a-option v-for="item in fhWhList2" :value="item.id + '-' + item.warehouseName">
                            {{ item.warehouseName }}
                        </a-option>
                    </a-select>
                </a-form-item>
                <!-- <a-form-item label="布笼">
                <a-select popup-container="html"  class="bacClass" :style="{ width: '10rem' }" @click="hjClick" @change="warehouseChange"  v-model="warehouseNameAanId" allow-clear>
                    <a-option v-for="item in fhWhList2" :value="item.id + '-' + item.warehouseName">
                        {{ item.warehouseName }}
                    </a-option>
                </a-select>
            </a-form-item> -->
            </a-form>
        </div>
        <a-table :columns="cgColumns" :data="cgData" :pagination="false" :bordered="{ cell: true }">
        </a-table>
    </a-modal>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue"
import { post, get } from "@/api/http"
import { localeText } from '@/api/aggridZw'

import Toolbar from "@/components/Toolbar.vue"
import { Message, Modal } from "@arco-design/web-vue"
import CorporateSectorCascader from '@/components/corporateSectorCascader/inde.vue'
import WHService from '@/service/wh'

const toolbarList: any = ref(['查询'])

const formXl = ref()

const fySelet: any = reactive({
    total: 10,
    size: 10,
    page: 1
})

const PageSize = (val: any) => {
    fySelet.page = val
    cxCgsList()
}

const change = (val: any) => {
    fySelet.size = val
    cxCgsList()
}

const fySelet2: any = reactive({
    total: 10,
    size: 10,
    page: 1
})

const PageSize2 = (val: any) => {
    fySelet2.page = val
    cxYcList()
}

const change2 = (val: any) => {
    fySelet2.size = val
    cxYcList()
}

const headerShow = ref(true)

const pageData: any = reactive({
    a: null,
    b: null,
    c: '',
    d: '',
    e: '',
})

const tableDataList = ref([])

const tableDataList2 = ref([])


const gridOption = ref()
const gridReady = (params: any) => {
    gridOption.value = params.api
}

const gridOption2 = ref()
const gridReady2 = (params: any) => {
    gridOption2.value = params.api
}


const defaultColDef = {
    sortable: true,
    resizable: true,
    filter: true,
}

const defaultColDef2 = {
    sortable: true,
    resizable: true,
    filter: true,
}


const localeText1: any = ref(localeText)

const tc = [
    {
        showRowGroup: 'hide_def',
        "headerName": "",
        "checkboxSelection": true,
        "headerCheckboxSelection": true,
        "pinned": "left",
        "width": 50,
        resizable: false,
        filter: false,
        sortable: false,
        minWidth: 50,
        maxWidth: 50,
        lockPosition: "left",
    },
    {
        headerName: '公司',
        field: 'companyName',
        // pinned: o['fixed'],
        width: 150,
    },
    {
        headerName: '退货单位',
        field: 'returnUnit',
        width: 150
    },
    {
        headerName: '退货单号',
        field: 'returnOrderNo',
        width: 150
    },
    {
        headerName: '客户',
        field: 'client',
        width: 150
    },
    {
        headerName: '流转卡号',
        field: 'flowCardCode',
        width: 150
    },
    {
        headerName: '条形码',
        field: 'barCode',
        width: 150
    },
    {
        headerName: '订单号',
        field: 'orderNo',
        width: 150
    },
    {
        headerName: '布号',
        field: 'clothCode',
        width: 150
    },
    {
        headerName: '品名',
        field: 'clothName',
        width: 150
    },
    {
        headerName: '色号',
        field: 'colorCode',
        width: 150
    },
    {
        headerName: '色称',
        field: 'colorName',
        width: 150
    },
    {
        headerName: '成品重量',
        field: 'finishedWeight',
        width: 150
    },
]

const selectHandler = () => {
    if (tabsKeys.value == 1) {
        cxYcList()
    } else if (tabsKeys.value == 2) {
        cxCgsList()
    }
}

const tabsKeys = ref(1)

const tabsChanges = (e: any) => {
    tabsKeys.value = e
    if (e == 1) {
        toolbarList.value = ['查询']
    } else if (e == 2) {
        toolbarList.value = ['查询', '入库']
    }
}


const cxCgsList = async () => {
    post({
        url: 'wms/finishedGoodsStock/getReturnPageInfo',
        data: {
            page: fySelet.page,
            size: fySelet.size,
        }
    }).then((res: any) => {
        gridOption2.value.setRowData(res.data)
        tableDataList2.value = res.data
        // fySelet.total = res.data.total
    })
}



const cxYcList = () => {
    post({
        url: 'wms/finishedGoodsStock/getPage',
        data: {
            page: fySelet2.page,
            size: fySelet2.size,
            companyId: pageData.a,
            warehouseId: pageData.b,
            stockInCode: pageData.e,
        }
    }).then((res: any) => {
        gridOption.value.setRowData(res.data.data)
        tableDataList.value = res.data.data
        fySelet2.total = res.data.total
    })
}

const cgRoutList: any = ref([])

const redersOut = () => {
    if (gridOption2.value.getSelectedRows().length == 0) {
        Message.error('请选择')
        return
    }
    for (let i = 0; i < gridOption2.value.getSelectedRows().length; i++) {
        if (gridOption2.value.getSelectedRows()[i].returnOrderNo != gridOption2.value.getSelectedRows()[0].returnOrderNo) {
            Message.error('请勾选相同退货单号')
            return
        }
    }
    cgData.value = JSON.parse(JSON.stringify(gridOption2.value.getSelectedRows()))

    post({
        url: 'wms/warehouse/getWarehouseByPara',
        data: {
            firmId: gridOption2.value.getSelectedRows()[0].companyId,
            warehouseTypeName: '成品仓',
            virtuallyState:0
        }
    }).then((res: any) => {
        if (res.code == 200) {
            cgRoutList.value = res.data
        }
    })

    cgOut.visible = true
    cgOut.title = '成品入库'
}

const cgOut = reactive({
    visible: false,
    title: '成品入库'
})

const cgHandleOk = async () => {

    if (warehouseNameAanId.value == '') {
        Message.error('请选择仓库')
        return false
    }
    cgData.value.forEach((res: any) => {
        res['companyId'] = pageData2.b
        res['companyName'] = pageData2.a
        res['warehouseId'] = pageData2.d
        res['warehouseName'] = pageData2.c
    })
    const { code, message } = await post({
        url: 'wms/finishedGoodsStock/addStock',
        data: cgData.value
    })
    if (code == 200) {
        cxYcList()
        cxCgsList()
        warehouseNameAanId.value = ''
        Message.success('成功')
        return true
    }
    Message.error(message)
    return false

}

const cgColumns: any = [
    {
        dataIndex: 'companyName',
        key: 'companyName',
        title: '公司',
        width: 150,
    },
    {
        dataIndex: 'returnUnit',
        key: 'returnUnit',
        title: '退货单位',
        width: 150,
    },
    {
        dataIndex: 'returnOrderNo',
        key: 'returnOrderNo',
        title: '退货单号',
        width: 150,
    },
    {
        dataIndex: 'client',
        key: 'client',
        title: '客户',
        width: 150,
    },
    {
        dataIndex: 'flowCardCode',
        key: 'flowCardCode',
        title: '流转卡号',
        width: 150,
    },
    {
        dataIndex: 'barCode',
        key: 'barCode',
        title: '条形码',
        width: 150,
    },
    {
        dataIndex: 'orderNo',
        key: 'orderNo',
        title: '订单号',
        width: 150,
    },
    {
        dataIndex: 'clothCode',
        key: 'clothCode',
        title: '布号',
        width: 150,
    },
    {
        dataIndex: 'clothName',
        key: 'clothName',
        title: '品名',
        width: 150,
    },
    {
        dataIndex: 'colorCode',
        key: 'colorCode',
        title: '色号',
        width: 150,
    },
    {
        dataIndex: 'colorName',
        key: 'colorName',
        title: '色称',
        width: 150,
    },
    {
        dataIndex: 'finishedWeight',
        key: 'finishedWeight',
        title: '成品重量',
        width: 150,
    },
]

const cgColumns2: any = [
    {
        dataIndex: 'orderNo',
        key: 'orderNo',
        title: '调拨单号',
        width: 150,
    },
    {
        dataIndex: 'outStockType',
        key: 'outStockType',
        title: '出库类型',
        width: 150,
    },
    {
        dataIndex: 'companyName',
        key: 'companyName',
        title: '发货公司',
        width: 150,
    },
    {
        dataIndex: 'warehouseTypeName',
        key: 'warehouseTypeName',
        title: '仓库类型',
        width: 150,
    },
    {
        dataIndex: 'warehouseName',
        key: 'warehouseName',
        title: '仓库名称',
        width: 150,
    },
    {
        dataIndex: 'inWarehouseTypeName',
        key: 'inWarehouseTypeName',
        title: '入库仓库类型',
        width: 150,
    },
    {
        dataIndex: 'inCompanyName',
        key: 'inCompanyName',
        title: '入库公司',
        width: 150,
    },
    {
        dataIndex: 'inWarehouseName',
        key: 'inWarehouseName',
        title: '入库仓库名称',
        width: 150,
    },
    {
        dataIndex: 'majorCategory',
        key: 'majorCategory',
        title: '物料大类 ',
        width: 150,
    },
    {
        dataIndex: 'subCategory',
        key: 'subCategory',
        title: '物料小类',
        width: 150,
    },
    {
        dataIndex: 'materialCode',
        key: 'materialCode',
        title: '物料编号',
        width: 150,
    },
    {
        dataIndex: 'materialName',
        key: 'materialName',
        title: '物料名称',
        width: 150,
    },
    {
        dataIndex: 'specification',
        key: 'specification',
        title: '规格',
        width: 150,
    },
    {
        dataIndex: 'totalNum',
        key: 'totalNum',
        title: '数量',
        width: 150,
    },
    {
        dataIndex: 'unit',
        key: 'unit',
        title: '单位',
        width: 150,
    },
    {
        dataIndex: 'grade',
        key: 'grade',
        slotName: 'grade',
        title: '等级',
        fixed: 'right',
        width: 150,
    },
    {
        dataIndex: 'updateTime',
        key: 'updateTime',
        title: '发货时间',
        width: 150,
    }
]

const warehouseNameAanId: any = ref('')
const warehouseChange = () => {
    pageData2.d = Number(warehouseNameAanId.value.split('-')[0])
    pageData2.c = warehouseNameAanId.value.split('-')[1]
    cgData.value.forEach((res: any) => {

    })
}

const cadelvs = () => {
    warehouseNameAanId.value = ''
    cgData.value = []
    cgOut.visible = false
}

const delSles = () => {
    Modal.confirm({
        title: '系统提示',
        content: '确认删除选中的数据?',
        onOk: () => {
            post({
                url: 'wms/ironwareInStock/delete',
                data: gridOption.value.getSelectedRows().map((res: any) => res.id)
            }).then((res: any) => {
                if (res.code == 200) {
                    Message.success('删除成功')
                    cxYcList()
                } else (
                    Message.error(res.message)
                )
            })
        },
    })
}

const audits = () => {
    Modal.confirm({
        title: '系统提示',
        content: '确认删除选中的数据?',
        onOk: () => {
            post({
                url: 'wms/ironwareInStock/audit',
                data: {
                    infoState: 2,
                    infos: gridOption.value.getSelectedRows().map((res: any) => {
                        return { id: res.id }
                    })
                }
            }).then((res: any) => {
                if (res.code == 200) {
                    Message.success('成功')
                    cxYcList()
                } else (
                    Message.error(res.message)
                )
            })
        },
    })
}

const cgData: any = ref([])
const cgData2: any = ref([])

const gradrList: any = ref([])

const gradeChange = () => {
    post({
        url: 'dict/dictTypeDetail/getListByMap',
        data: {
            codeType: 26,
            detailName: ''
        }
    }).then(({ data, code }: any) => {
        if (code === 200) {
            gradrList.value = data;
        }
    })
}
const configCascader = ref<{}>({
    corporateWidth: '200px', //公司宽度
    sectorWidth: '180px', //部门宽度
    corporateRquired: true, //公司是否必填
    sectorRquired: false, //部门是否必填
    size: 'mini', //选择框大小'mini' | 'small' | 'medium' | 'large'
    corporateName: '选择公司', //公司label
    sectorName: '选择部门', //部门label
    cascaderShow: 'sector', //配置部门不显示
})
const changeCascader = (data: any) => {
    const { corporateId, sectorId, sectorLabel, corporateLabel } = data
    pageData.c = corporateId
}
const configCascader2 = ref<{}>({
    corporateWidth: '200px', //公司宽度
    sectorWidth: '180px', //部门宽度
    corporateRquired: false, //公司是否必填
    sectorRquired: false, //部门是否必填
    size: 'mini', //选择框大小'mini' | 'small' | 'medium' | 'large'
    corporateName: '选择公司', //公司label
    sectorName: '选择部门', //部门label
    cascaderShow: 'sector', //配置部门不显示
})
const whService = new WHService()
const fhWhList: any = whService.whList
const changeCascader2 = (data: any) => {
    const { corporateId, sectorId, sectorLabel, corporateLabel } = data
    pageData.b = ""
    pageData.a = corporateId
    whService.getWarhouseListByFirmId(corporateId, '成品仓')
}

const pageData2: any = reactive({
    a: '',
    b: null,
    c: '',
    d: null
})

const whService2 = new WHService()
const fhWhList2: any = whService2.whList

const changeCascader3 = (data: any) => {
    const { corporateId, sectorId, sectorLabel, corporateLabel } = data
    pageData2.c = ''
    pageData2.d = null
    warehouseNameAanId.value = ''
    pageData2.a = corporateLabel
    pageData2.b = corporateId
    whService2.getWarhouseListByFirmId(corporateId, '成品仓')
}

const hjClick = async () => {
    const res = await post({
        url: 'wms/shelve/getFieldValue',
        data: {
            shelveName: ''
        }
    })
}
</script>
<script lang="ts">
export default {
    name: "ReceiptList4",
}
</script>